package com.demo.crud.component;


import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

/**
 * Created with IntelliJ IDEA.
 *
 * @author: keen
 * Date: 2019-06-04
 * Time: 23:11
 */
public class MyLocalResolver implements LocaleResolver {
    private static final Locale DEFAULT_LOCALE=new Locale("zh","CN");
    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for(Cookie cookie:cookies){
            if("lang".equals(cookie.getName())){
                String[] langAndCountry = cookie.getValue().split("_");
                if(langAndCountry.length==2){
                    return new Locale(langAndCountry[0],langAndCountry[1]);
                }
            }
        }
        return DEFAULT_LOCALE;
    }

    @Override
    public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

    }
}
